/**
 *
 *  Kelas player berisi getter, setter, dan method-method seperti acak, bangkrut, dan memindahkan bidak
 * 
 */

package model.players;
import model.board.*;
import helper.*;
import controller.permainan.*;
import java.awt.Color;
import java.io.Serializable;
import javax.swing.ImageIcon;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;

public class Player implements Serializable
{
    //Attribute
    private String nama = "";
    private int no = 0;
    private String ikon = new String ("");
    private boolean HUMAN ;
    private int posisi = 1; //di taro di start

    private int hunger = 20;
    private int fun = 20;
    private int uang = 50000;
    public int jum_aset = 0;
    public int aset[] = new int[100];
    
    public boolean sudah_bayar = true;
    public boolean boleh_acak;
    public boolean masuk_penjara = false ;
    
    public Player()
    {
        
    }

    //konstruktor
    public Player (int _no, String _nama, String _ikon, boolean _human)
    {
        no = _no;
        nama = _nama;
        ikon = new String (_ikon);
        HUMAN = _human ;
    }

    //------------------------------------GETTER--------------------------------
    //MENGAMBIL NOMOR PEMAIN
    public int getNo()
    {
        return this.no;
    }

    //MENGAMBIL NAMA PEMAIN
    public String getNama()
    {
        return this.nama ;
    }

    //MENGAMBIL ICON PEMAIN
    public String getIkon()
    {
        return this.ikon;
    }

    //MENGAMBIL ICON BESAR
    public ImageIcon getLargeImage()
    {
        return SearchEngine.createImageIcon(this.getIkon());
    }

    //MENGAMBIL ICON KECIL
    public ImageIcon getSmallImage()
    {
        return SearchEngine.createImageIcon(this.getIkon().substring(0,7)+"small.jpg");
    }

    //MENGAMBIL WARNA 1
    public Color getColor()
    {
        Color retval = null;
        if(this.getIkon().equals("images0.gif"))
            retval = new Color(255, 51, 51);
        else if(this.getIkon().equals("images1.gif"))
            retval = new Color(102, 255, 0);
        else if(this.getIkon().equals("images2.gif"))
            retval = new Color(51, 204, 255);
        else if(this.getIkon().equals("images3.gif"))
            retval = new Color(255, 255, 0);

        return retval;
    }

    //MENGAMBIL WARNA UNTUK NGEWARNAIN KOTAK YANG PUNYANYA DIA
    public Color getSoftColor()
    {
        Color retval = null;
        if(this.getIkon().equals("images0.gif"))
            retval = new Color(255, 153, 153);
        else if(this.getIkon().equals("images1.gif"))
            retval = new Color(153, 255, 153);
        else if(this.getIkon().equals("images2.gif"))
            retval = new Color(102,255,204);
        else if(this.getIkon().equals("images3.gif"))
            retval = new Color(255, 255, 204);

        return retval;
    }

    //MENGAMBIL POSISI PEMAIN
    public int getPosisi()
    {
        return this.posisi;
    }

    //MENGAMBIL HUNGER
    public int getHunger()
    {
        return this.hunger ;
    }

    //MENGAMBIL FUNG
    public int getFun()
    {
        return this.fun ;
    }

    //MENGAMBIL UANG
    public int getUang()
    {
        return this.uang ;
    }

    public boolean getHuman()
    {
        return this.HUMAN;
    }

    //---------------------------------SETTER-----------------------------------
    //MENGUBAH NAMA
    public void setNama(String name)
    {
        this.nama = name;
    }

    //MENGUBAH UANG
    public void setUang (int uang)
    {
        this.uang = uang;
    }

    //MENGUBAH POSISI
    public void setPosisi (int posisi)
    {
        this.posisi = posisi;
    }

    //MENGUBAH FUN
    public void setFun (int fun)
    {
        this.fun = fun;
    }

    //MENGUBAH HUNGER
    public void setHunger (int hunger)
    {
        this.hunger= hunger;
    }

    public void setIkon (String icon)
    {
        this.ikon = icon;
    }

    public void setHuman (boolean _human)
    {
        this.HUMAN = _human;
    }

   
    //---------------------------------METHOD-----------------------------------
//==============================================================================
//ACAK KETIKA TIDAK DI KOTAK PENJARA
    public int dadu1 ;
    public int dadu2 ;
    public int jumlah_dadu ;

    public String acak()
    {
        String retval = new String ("");
        
        dadu1 = RandomRange.getRandomInteger();
        dadu2 = RandomRange.getRandomInteger();
        retval = new String (retval.concat("Dices : " +dadu1+" & "+dadu2));
        jumlah_dadu = dadu1 + dadu2 ;

        //KONDISI : ANGKA KEMBAR
        if (dadu1==dadu2)
        {
            boleh_acak = true;
        }
        //KONDISI : HUNGER TIDAK CUKUP, BUKAN ANGKA KEMBAR
        else
        {
            boleh_acak = false;
        }
        
        return retval;
    }

    //MEMINDAHKAN BIDAK
    public void moveYourPlayer(final Permainan2 game, final int jumlah_dadu)
    {
        //KONDISI : HUNGER MENCUKUPI
        if (this.getHunger() > jumlah_dadu)
        {
            Timer t = new Timer();
            t.scheduleAtFixedRate(new TimerTask()
            {
                private int i = 0;
                @Override
                public void run()
                {
                    if(posisi < 32)
                    {
                        posisi = posisi + 1 ;
                    }
                    //KONDISI : MELALU KOTAK START
                    else
                    {
                        posisi = 1 ;
                        addUang(10000);
                        game.HistoryWrite(game.G.current_plyr().getNama()+ " gets 10000");
                        game.AssignAsThisPlyr(game.G.urutan_current_plyr);
                    }
                    game.ShowPlyrAllPosition();
                    i++;
                    if (i == jumlah_dadu)
                    {
                        this.cancel();
                        if (HUMAN)
                        {
                            game.CekKotak();
                        }
                        else
                        {
                            //dia pake cek kotak yang ada di ai_plyr
                        }

                    }
                }
            }, 0, 500);
            this.hunger = this.hunger - (jumlah_dadu/2);
        }
    }

//==============================================================================
//MENGECEK APAKAH UANG CUKUP (UNTUK KASUS MAKAN, REKREASI)
    public boolean cukup (int minus)
    {
        return ((this.uang - minus) >= 0);
    }

//==============================================================================
//MENGECEK APAKAH SANGGUP BAYAR
    public boolean bayar (int pengeluaran)
    {
        boolean retval = false;

        if  ((this.uang-pengeluaran) <0)
        {
            JOptionPane.showMessageDialog(null, nama+" NSF!! "+this.nama+" losts 5 fun..T.T");
            this.fun -= 5;
            this.sudah_bayar = false;
        }
        else
        {
            this.uang = this.uang-pengeluaran;
            retval = true;
            this.fun -= 1;
            this.sudah_bayar = true;
        }
        return retval;
    }

//==============================================================================
//MENGECEK HUNGER KRITIS
    public boolean criticalHunger ()
    {
       return ((this.hunger < 10) && (this.hunger != 0));
    }

//==============================================================================
//MENGECEK FUN KRITIS
    public boolean criticalFun()
    {
        return ((this.fun<10) && (this.fun != 0));
    }

//==============================================================================
//MENGECEK FUN == 0
    public boolean bored()
    {
        boolean retval = false ;
        if (this.fun <= 0)
        {
            this.setFun(0);
            retval = true ;
        }
        return retval;
    }

//==============================================================================
//MENGECEK HUNGER == 0
    public boolean hungry()
    {
        boolean retval = false ;
        if (this.hunger <= 0)
        {
            this.setHunger(0);
            retval = true ;
        }
        return retval;
    }

//==============================================================================
//PENANGANAN HUNGER KETIKA MAKAN
    public void makan (int masakan)
    {
        if ((this.hunger+masakan) >=20)
            this.hunger = 20;
        else
            this.hunger = this.hunger + masakan;
    }

//==============================================================================
//PENANGANAN FUN KETIKA REKREASI
    public void rekreasi (int wisata)
    {
        if ((this.fun+wisata) >=20)
            this.fun = 20;
        else
            this.fun = this.fun + wisata;
    }

//==============================================================================
//LIHAT
    public String getAttr()
    {
        String retval = new String ("");
        retval = retval.concat("Fund\t:"+Integer.toString(this.uang));
        retval = retval.concat("\nHunger\t:"+Integer.toString(this.hunger));
        retval = retval.concat("\nFun\t:"+Integer.toString(this.fun));
        return retval;
    }

    public String getAset(Kotak[] Board)
    {
        String retval = new String ("");
        retval = retval.concat("Assets\t:");
        retval = retval.concat("\n    Asset       |     Buildings     ");
        retval = retval.concat("\n----------------|------------------ ");

        for (int i = 1 ; i <= jum_aset; i++)
        {
            if (Board[aset[i]].getTipe() == 2)
            {
                LokasiPariwisata LP = (LokasiPariwisata) Board[aset[i]];
                retval = retval.concat("\n"+(LP.getNama())+" | "+LP.getJum_villa()+" Villa  "+LP.getJum_hotel()+" hotel");
            }
        }
        for (int i = 1 ; i <= jum_aset; i++)
        {
            if (Board[this.aset[i]].getTipe() == 4)
                retval = retval.concat("\nAirport "+Board[this.aset[i]].getNama());
        }
        retval = retval.concat("\n----------------|------------------ \n");
        return retval;
    }

//==============================================================================
//MENAMBAH UANG 
  public void addUang (int money)
    {
        this.uang = this.uang + money;
    }

//==============================================================================
//BANGKRUT
    public void bangkrut(Kotak[] Board)
    {
        //MENETRALKAN ASET
        for (int i = 1; i <= this.jum_aset; i++)
        {
            Board[this.aset[i]].setOwner(null);
            if (Board[this.aset[i]].getTipe() == 2)
            {
                LokasiPariwisata LP = (LokasiPariwisata)  Board[this.aset[i]];
                LP.setJumvilla(0);
                LP.setJumhotel(0);
            }
        }
        System.out.println("Bangkrut");
   }

   public static void puts (String S)
    {
        System.out.println(S);
    }

   //===========================================================================
   //MAIN

   public static void main (String[]args)
   {
       puts("Main untuk kelas Player");
       Kotak[] Board = new Kotak[33];
       Permainan2 P = new Permainan2();

       Player P1 = new Player (1, "Lisa", new String ("images0.gif"),true);
       
       //TEST SETTER DAN GETTER
       puts(P1.getAttr());
       
       P1.setNama("Anu"); puts(P1.getNama());
       P1.setIkon("images2.gif"); puts(P1.getIkon());

       if (P1.hungry() || (P1.criticalHunger()))
            P1.setHunger(20); System.out.println(P1.getHunger());
       P1.makan(10); System.out.println(P1.getHunger());

       if (P1.bored() || (P1.criticalFun()))
            P1.setFun(10); System.out.println(P1.getFun());
       P1.rekreasi(10); System.out.println(P1.getFun());

       P1.setUang(20000); System.out.println(P1.getUang());
       P1.addUang(5000); System.out.println(P1.getUang());
       P1.bayar(5000); System.out.println(P1.getUang());

       P1.setPosisi(24); System.out.println(P1.getPosisi());

       //TEST GETTER UNTUK IMAGE DAN WARNA
       puts(P1.getLargeImage().toString());
       puts(P1.getSmallImage().toString());
       puts(P1.getColor().toString());
       puts(P1.getSoftColor().toString());
      
       //TEST ACAK
       puts(P1.acak());
       
       //TEST MOVE
       P1.moveYourPlayer(P,10);
       //TEST GETTER ASET
       System.out.println(P1.getAttr());
       System.out.println(P1.getAset(Board));
       //TEST BANGKRUT
       P1.bangkrut(Board);
   }
 
}
